package com.baizhi.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baizhi.entity.PmsBrand;
import com.baizhi.entity.PmsCategory;
import com.baizhi.entity.PmsCategoryBrandRelation;
import com.baizhi.mapper.PmsBrandMapper;
import com.baizhi.mapper.PmsCategoryBrandRelationMapper;
import com.baizhi.mapper.PmsCategoryMapper;
import com.baizhi.service.PmsBrandService;
import com.baizhi.vo.PmsBrandUpdateVo;
import com.baizhi.vo.PmsBrandVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yu
 */
@Service
public class PmsBrandServiceImpl implements PmsBrandService {
    @Autowired
    private PmsBrandMapper pmsBrandMapper;
    @Autowired
    private PmsCategoryBrandRelationMapper pmsCategoryBrandRelationMapper;
    @Autowired
    private PmsCategoryMapper pmsCategoryMapper;

    private String PROJECTNAME="bzmall";
    @Override
    /*未修改*/
    public Map showAll(Integer pageNum, Integer pageSize) {
        HashMap<String, Object> map = new HashMap<>();
        List<PmsBrandVo> pmsBrandVos = pmsBrandMapper.selectBrandCategoryName((pageNum - 1) * pageSize, pageSize);
        Integer count = pmsBrandMapper.selectCount(null);
        map.put("code",0);
        map.put("count",count);
        map.put("data",pmsBrandVos);
        return map;
    }


    @Override
    public void insert(PmsBrandVo pmsBrandVo) {
        PmsBrand pmsBrand = new PmsBrand();
        BeanUtils.copyProperties(pmsBrandVo,pmsBrand);
        pmsBrandMapper.insert(pmsBrand);
        System.out.println(pmsBrand.getBrandId());
        QueryWrapper<PmsCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("name",pmsBrandVo.getCategoryName());
        PmsCategory pmsCategory = pmsCategoryMapper.selectOne(wrapper);
        PmsCategoryBrandRelation pmsCategoryBrandRelation = new PmsCategoryBrandRelation();
        pmsCategoryBrandRelation.setCategoryId(pmsCategory.getCatId());
        pmsCategoryBrandRelation.setCategoryName(pmsCategory.getName());
        pmsCategoryBrandRelation.setBrandId(pmsBrand.getBrandId());
        pmsCategoryBrandRelation.setBrandName(pmsBrandVo.getName());
        System.out.println("pmsCategoryBrandRelation = " + pmsCategoryBrandRelation);
        pmsCategoryBrandRelationMapper.insert(pmsCategoryBrandRelation);

    }

    @Override
    public void delete(Integer id) {
        pmsBrandMapper.deleteById(id);
    }

    @Override
    public void update(PmsBrandUpdateVo pmsBrandUpdateVo) {
        PmsBrand pmsBrand = new PmsBrand();
        BeanUtils.copyProperties(pmsBrandUpdateVo,pmsBrand);
        QueryWrapper<PmsCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("name",pmsBrandUpdateVo.getCategoryName());
        PmsCategory pmsCategory = pmsCategoryMapper.selectOne(wrapper);
        PmsCategoryBrandRelation pmsCategoryBrandRelation = new PmsCategoryBrandRelation();
        pmsCategoryBrandRelation.setBrandId(pmsBrandUpdateVo.getBrandId());
        pmsCategoryBrandRelation.setBrandName(pmsBrandUpdateVo.getName());
        pmsCategoryBrandRelation.setCategoryId(pmsCategory.getCatId());
        pmsCategoryBrandRelation.setCategoryName(pmsCategory.getName());
        QueryWrapper<PmsCategoryBrandRelation> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("brand_id",pmsBrandUpdateVo.getBrandId()).eq("category_name",pmsBrandUpdateVo.getOldCategoryName());
        pmsBrandMapper.updateById(pmsBrand);
        pmsCategoryBrandRelationMapper.update(pmsCategoryBrandRelation,wrapper1);
    }

    @Override
    public Workbook downloadExcel() {
        List<PmsBrand> pmsBrands = pmsBrandMapper.selectList(null);
        for (PmsBrand pmsBrand : pmsBrands) {
            pmsBrand.setLogo(pmsBrand.getLogo());
        }
        ExportParams exportParams = new ExportParams("全部品牌信息","品牌信息");
        Workbook sheets = ExcelExportUtil.exportExcel(exportParams, PmsBrand.class, pmsBrands);
        return sheets;
    }

    @Override
    public void uploadExcel(InputStream inputStream) {
        ImportParams importParams = new ImportParams();
        importParams.setHeadRows(1);
        importParams.setTitleRows(1);
        try {
            List<PmsBrand> pmsBrands = ExcelImportUtil.importExcel(inputStream, PmsBrand.class, importParams);
            for (PmsBrand pmsBrand : pmsBrands) {
                pmsBrand.setLogo(pmsBrand.getLogo().split("webapp")[1]);
                pmsBrandMapper.insert(pmsBrand);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
